package cn.com.mayue.guava.ranges;

import cn.com.mayue.guava.Person;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Range;
import org.junit.Test;

/**
 * Created by mayue on 2016/12/22.
 */
public class RangeTest {
    @Test
    public void test() {
        //创建一个指定范围的区间
        //包括开始和结束值
        Range<Integer> range = Range.closed(1, 10);
        //不包括开始和结束值
        Range<Integer> open = Range.open(1, 10);

        System.out.println(range.contains(5));
        System.out.println(range.contains(10));
        System.out.println(range.contains(1));
        System.out.println(open.contains(1));
        System.out.println(open.contains(5));
        System.out.println(open.contains(10));
    }

    /**
     * 区间的使用场景
     */
    @Test
    public void testPredicate() {
        Person personOne = new Person("testOne", 10);
        Person personTwo = new Person("testTwo", 20);
        Person personThree = new Person("testThree", 30);
        //通过Range和Function创建一个Predicate，根据Range对数据进行过滤判断
        Range<Integer> ageRange = Range.closed(20, 25);
        Function<Person, Integer> ageFunction = new Function<Person, Integer>() {
            public Integer apply(Person person) {
                return person.getAge();
            }
        };

        Predicate<Person> predicate = Predicates.compose(ageRange, ageFunction);

        System.out.println(predicate.apply(personOne));
        System.out.println(predicate.apply(personTwo));
        System.out.println(predicate.apply(personThree));
    }
}
